package FlappyBirds;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;

/**
 * 分数面版
 * 
 * @author ll
 */
public class HelpPanel extends JPanel {

	int score = 0;
	int best = 0;
	JLabel scoreLabel = new JLabel("0");// 标签
	JLabel bestLabel = new JLabel("0");
	FileInputStream fin = null;
	FileOutputStream fout = null;
	DataInputStream din = null;// 使用需用到FileInputStream
	DataOutputStream dout = null;

	public HelpPanel() {
		Dimension size = new Dimension(600, 20);// 设置尺寸
		this.setPreferredSize(size);
		this.setVisible(true);
		this.setLayout(new GridLayout(1, 4, 4, 4));// 数据分别为行数,列数,行间距,列间距
		this.add(new JLabel("score:"));// 表示目前得分
		this.add(scoreLabel);// 具体数据
		this.add(new JLabel("best:"));// 表示最好得分
		this.add(bestLabel);// 具体数据
		getRecord();
	}

	public void setScore(int score) {
		this.score = score;
		scoreLabel.setText(score + "");
	}

	/**
	 * 从文件中读取最高记录
	 * 
	 * @return
	 */
	public int getRecord() {
		File file = new File("D:\\Game");

		if (!file.exists()) {// 如果目录不存在，则新建一个目录
			file.mkdirs();// 可以创建多级目录，父目录不一定存在
		}
		File record = new File("D:\\Game\\FlappyBirdGame\\flappyBirdRecord.txt");

		try {
			if (!record.exists()) {// 如果文本不存在,则新建文本
				record.createNewFile();// 新建
				fout = new FileOutputStream(record);
				dout = new DataOutputStream(fout);
				String s = "0";
				dout.writeBytes(s);
				System.out.println(record.isFile());
				;
			}
			// 如果存在，则读取数据
			fin = new FileInputStream(record);
			din = new DataInputStream(fin);// 实例化
			String str = din.readLine();// 读
			best = Integer.parseInt(str);// 转为int型
			bestLabel.setText("" + best);// 写入标签

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {// 关闭输入输出流
				if (fin != null) {
					fin.close();
				}
				if (din != null) {
					din.close();
				}
				if (fout != null) {
					fout.close();
				}
				if (dout != null) {
					dout.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return best;
	}

	/**
	 * 写入记录
	 * 
	 * @param score
	 */
	public void writeRecord(int score) {
		File record = new File("D:\\eclipse-workspace\\FlappyBird\\flappyBirdRecord.txt");

		try {
			// 清空原有记录
			FileWriter fileWriter = new FileWriter(record);// 如果需不覆盖可以new FileWriter(record,true);
			fileWriter.write("");
			fileWriter.flush();// 将缓冲区内容写入
			fileWriter.close();

			fout = new FileOutputStream(record);
			dout = new DataOutputStream(fout);
			String s = score + "";// 转为String型
			dout.writeBytes(s);// 写入
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {// 关闭输入输出
				if (fout != null) {
					fout.close();
				}
				if (dout != null) {
					dout.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}
